package com.zjdiepu.www.biz.dh;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.dh.DhDepositMoneyMapper;
import com.zjdiepu.www.ibiz.dh.IDhDepositMoneyService;
import com.zjdiepu.www.model.dh.dto.DhDepositMoney;
import com.zjdiepu.www.model.dh.ext.DhDepositMoneyExt;
import com.zjdiepu.www.model.dh.vo.DhDepositMoneyVo;

/**
 * Service层操作实现<br/>
 * @author zhangpp<br/>
 * @time 2017年05月16日 15时56分<br/>
 * @since 1.0<br/>
 */
@Service
public class DhDepositMoneyServiceImpl extends BaseService implements IDhDepositMoneyService {
	public static final Logger logger = LoggerFactory.getLogger(DhDepositMoneyServiceImpl.class);

	@Autowired
	private DhDepositMoneyMapper dhDepositMoneyMapper;
	
	@Override
	public void saveUpdateAndDelete(DhDepositMoney dhDepositMoney, OperateType operType) {
		switch (operType) {
		case add:
			dhDepositMoney.initInsertData(dhDepositMoney.getModifier(), dhDepositMoney.getModifierName());
			dhDepositMoneyMapper.insert(dhDepositMoney);
			break;
		case edit:
			dhDepositMoney.initUpdateData(dhDepositMoney.getModifier(), dhDepositMoney.getModifierName());
			// 影响的记录数
			int updateCount = dhDepositMoneyMapper.updateByPrimaryKeySelective(dhDepositMoney);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			dhDepositMoney.initDeleteData(dhDepositMoney.getModifier(), dhDepositMoney.getModifierName());
			// 影响的记录数
			int deleteCount = dhDepositMoneyMapper.updateByPrimaryKeySelective(dhDepositMoney);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<DhDepositMoney> selectByExt(DhDepositMoneyExt dhDepositMoneyExt) {
		return dhDepositMoneyMapper.selectByExt(dhDepositMoneyExt);
	}
	
	@Override
	public int countByExt(DhDepositMoneyExt dhDepositMoneyExt){
		return dhDepositMoneyMapper.countByExt(dhDepositMoneyExt);
	}
	
	@Override
	public DhDepositMoney selectById(String id) {
		return dhDepositMoneyMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			dhDepositMoneyMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(DhDepositMoney dhDepositMoney) {
		return dhDepositMoneyMapper.countRefined(dhDepositMoney);
	}

	@Override
	public DhDepositMoney selectRefined(DhDepositMoney dhDepositMoney) {
		return dhDepositMoneyMapper.selectRefined(dhDepositMoney);
	}
	
	@Override
	public int deletedRefined(DhDepositMoney dhDepositMoney) {
		return dhDepositMoneyMapper.deletedRefined(dhDepositMoney);
	}

	@Override
	public boolean repeat(DhDepositMoney dhDepositMoney) {
		int count = countRefined(dhDepositMoney);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(dhDepositMoney.getId())) {
			return true;
		}
		DhDepositMoney dbItem = selectRefined(dhDepositMoney);
		if(null == dbItem) {
			return false;
		}
		return !dhDepositMoney.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return dhDepositMoneyMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<DhDepositMoney> dhDepositMoneys) {
		return dhDepositMoneyMapper.insertBatch(dhDepositMoneys);
	}

	@Override
	public List<DhDepositMoneyVo> downloadExcel(String busiId) {
		
		return dhDepositMoneyMapper.downloadExcel(busiId);
	}
}
